import babel from '@rollup/plugin-babel'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import postcss from 'rollup-plugin-postcss'
import { terser } from 'rollup-plugin-terser'
import serve from 'rollup-plugin-serve'
import pkg from './package.json'
const banner = '/*!\n' + ` * html-replacer.js v${pkg.version}\n` + ` * @cj_zheng\n` + ' * Released under the MIT License.\n' + ' */'
export default function () {
  const input = './index.js'
  const output = []
  const plugins = [
    postcss({
      extensions: ['.css'],
      minimize: true
    }),
    resolve(),
    commonjs()
  ]
  const envName = process.env.NODE_ENV
  switch (envName) {
    case 'production':
      output.push(
        {
          file: './dist/htmlReplacer.js',
          format: 'umd',
          name: 'htmlReplacer',
          esModule: false,
          banner
        },
        {
          file: './dist/htmlReplacer.esm.js',
          format: 'es',
          banner
        }
      )
      plugins.push(terser(), babel({ babelHelpers: 'runtime' }))
      return {
        input,
        output,
        plugins,
        external: id => id.includes('@babel/runtime')
      }
    case 'preview':
      output.push({
        file: './examples/htmlReplacer.preview.js',
        format: 'iife',
        name: 'htmlReplacer',
        esModule: false,
        banner
      })
      plugins.push(
        babel({ babelHelpers: 'bundled' }),
        serve({
          open: true,
          contentBase: 'examples',
          historyApiFallback: '/highdense-test.html',
          host: 'localhost',
          port: 9999
        })
      )
      return {
        input,
        output,
        plugins
      }
    default:
      break
  }
}
